package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Music;
import com.qf.service.Impl.MusicServiceImpl;
import com.qf.service.MusicService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 音乐找到所有servlet
 *
 * @author 阿亘
 * @date 2023/04/20
 */
@WebServlet("/musicLastSongServlet.action")
public class MusicLastSongServlet extends HttpServlet {
    private MusicService musicService = new MusicServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取上一首，方法的编写（参数 + 返回值）
        //获取前端传过来的当前正在播放歌曲的id
        String musicId = req.getParameter("musicId");
        //判断
        if(musicId != null){
            //通过当前歌曲的id返回下一首歌曲的对象
            Music nextMusic = musicService.lastSong(musicId);
            //转换为Json
            String jsonNextSong = JSON.toJSONString(nextMusic);
            //返回
            resp.getWriter().write(jsonNextSong);
        }else{
            throw new RuntimeException("歌曲id不能为空");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
